package cc.surui
{
	import cc.surui.common.event.UIEventer;
	import cc.surui.cpp.CPPFSCommand;
	import cc.surui.logic.AppleRunner;
	import cc.surui.ui.UIApple;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.external.ExternalInterface;

	/**
	 * ...
	 * @author Surui
	 */
	[Frame(factoryClass = "cc.surui.Preloader")]
	public class Main extends Sprite 
	{
		private var _uiApple:UIApple = new UIApple();
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			this.addChild(_uiApple);
			_uiApple.init();
			UIEventer.addListener(UIEventer.START_BUTTON_CLICK, startRun);
			this.addEventListener(Event.ENTER_FRAME, gameLogicLoop);
		}
		
		private function gameLogicLoop(event:Event):void {
			AppleRunner.self.update();
		}
		
		private function startRun():void {
			//AppleRunner.self.start();
			CPPFSCommand.self.initCppCMD();
			CPPFSCommand.self.call("MsgBox","{cmd:'START_RUN',params:{id:123456}}",cppCallback); 
		}
		
		private function cppCallback( msg:String ):void {
			trace( msg );
			AppleRunner.self.start();
		}

	}

}